package com.danco.training.hoteladmin.storage;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import com.danco.training.TextFileWorker;
import com.danco.training.hoteladmin.core.Client;
import com.danco.training.hoteladmin.core.Room;

public class Storage {
 
	private ArrayList<Client> clientArray = new ArrayList<Client>();
	private ArrayList<Room> roomArray = new ArrayList<Room>();
	private String[] clientStringArray;
	private String[] roomStringArray;
			
	public ArrayList<Client> getClientArray() {
		return clientArray;
	}

	public void setClientArray(ArrayList<Client> clientArray) {
		this.clientArray = clientArray;
	}

	public ArrayList<Room> getRoomArray() {
		return roomArray;
	}

	public void setRoomArray(ArrayList<Room> roomArray) {
		this.roomArray = roomArray;
	}
	
	public String[] getClientStringArray() {
		return clientStringArray;
	}

	public void setClientStringArray(String[] clientStringArray) {
		this.clientStringArray = clientStringArray;
	}

	public String[] getRoomStringArray() {
		return roomStringArray;
	}

	public void setRoomStringArray(String[] roomStringArray) {
		this.roomStringArray = roomStringArray;
	}

	public ArrayList<Client> clientsReadFromFile(String path){		
		TextFileWorker fileWorker = new TextFileWorker(path);
		clientStringArray = fileWorker.readFromFile();
		for (int i=0;i<clientStringArray.length;i++){
			if  (clientStringArray[i] !=null){				
				String temp[]=clientStringArray[i].split(" ");			
				clientArray.add(new Client(temp[0], 					
					new GregorianCalendar(Integer.parseInt(temp[3]),
						Integer.parseInt(temp[2]), Integer.parseInt(temp[1])),new GregorianCalendar
							(Integer.parseInt(temp[6]),Integer.parseInt(temp[5]), Integer.parseInt(temp[4]))));
			}			
		}		
		return clientArray;		
	}
	
	public ArrayList<Room> roomsReadFromFile(String path1){		
		TextFileWorker fileWorker = new TextFileWorker(path1);
		roomStringArray = fileWorker.readFromFile();		
		for (int i=0;i<roomStringArray.length;i++){
			if  (roomStringArray[i] !=null){
				String temp[]=roomStringArray[i].split(" ");
					roomArray.add(new Room(Integer.parseInt(temp[0]),
						Integer.parseInt(temp[1]),Integer.parseInt(temp[2])));
			}				
		}		
		return roomArray;
	}
	
	public void clientsWriteToFile(String path){
		TextFileWorker fileWorker = new TextFileWorker(path);
		clientStringArray= new String[clientArray.size()];
		for(int i=0;i<clientArray.size();i++){						
			clientStringArray[i]=clientArray.get(i).getName()+" "
				+clientArray.get(i).getDateOfSettlement().get(Calendar.DATE)+" "
					+clientArray.get(i).getDateOfSettlement().get(Calendar.MONTH)+" "
						+clientArray.get(i).getDateOfSettlement().get(Calendar.YEAR)+" "
							+clientArray.get(i).getDateOfEviction().get(Calendar.DATE)+" "
								+clientArray.get(i).getDateOfEviction().get(Calendar.MONTH)+" "
									+clientArray.get(i).getDateOfEviction().get(Calendar.YEAR);				
			
		}		
		fileWorker.writeToFile(clientStringArray);
	}
	
	public void roomsWriteToFile(String path){
		TextFileWorker fileWorker = new TextFileWorker(path);
		roomStringArray= new String[roomArray.size()];
		for(int i=0;i<roomArray.size();i++){			
				roomStringArray[i]=String.valueOf(roomArray.get(i).getRoomPrice())+" "+
					String.valueOf(roomArray.get(i).getCapacity())+" "+
						String.valueOf(roomArray.get(i).getCountOfStars());			
		}		
		fileWorker.writeToFile(roomStringArray);
	}	
	
	
	
	public void showRoomArray(){
		for (int i=0;i<roomArray.size();i++){			
			System.out.println(roomArray.get(i).getRoomPrice() + " " + 
				roomArray.get(i).getCapacity() + " " + roomArray.get(i).getCountOfStars());
			
		}
	}
	
	public void showClientArray(){
		for (int i=0;i<clientArray.size();i++){
			System.out.println(clientArray.get(i).getName()+" "
				+clientArray.get(i).getDateOfSettlement().get(Calendar.DATE)+" "
					+clientArray.get(i).getDateOfSettlement().get(Calendar.MONTH)+" "
						+clientArray.get(i).getDateOfSettlement().get(Calendar.YEAR)+" "
							+clientArray.get(i).getDateOfEviction().get(Calendar.DATE)+" "
								+clientArray.get(i).getDateOfEviction().get(Calendar.MONTH)+" "
									+clientArray.get(i).getDateOfEviction().get(Calendar.YEAR));
			 
		}
		
	}

	public void showFreeRooms(){
		int temp=0;
		for (int i=0;i<roomArray.size();i++){
		if (roomArray.get(i).isFree()==true){
			temp++;
		}
		System.out.println(temp);
		}
	}
	
	public void showCountOfClients(){
		System.out.println(clientArray.size());
	}
	
	public void changeRoomStatus(Room room){
		if (room.isWorkingStatus()==true) room.setWorkingStatus(false);
		else room.setWorkingStatus(true);
	}
	
	public void changeRoomPrice(Room room,int price){		
		 room.setRoomPrice(price);
	}
	
	public void addRoom(Room room){
		roomArray.add(room);
		
	}
	
	public void addClient(Client client){
		clientArray.add(client);
	}
	
	public void Settle(int clientID,int roomID){
		clientArray.get(clientID).setRoomId(roomID);
		roomArray.get(roomID).setClientId(clientID);
		roomArray.get(roomID).setFree(false);
		
	}
	
	public void Evict(int clientID,int roomID){
		clientArray.get(clientID).setRoomId(0);
		roomArray.get(roomID).setClientId(0);
		roomArray.get(roomID).setFree(true);
	}
}